package com.shimao.celnet.payment.quartz;

import com.baiwang.bop.respose.entity.InvoiceQueryResponse;
import com.shimao.celnet.payment.controller.apinotcheck.CallOut_invOpenRetInfoOutController;
import com.shimao.celnet.payment.controller.apinotcheck.CallOut_invPdfOutController;
import com.shimao.celnet.payment.controller.apinotcheck.CallOut_invoiceQueryOutController;
import com.shimao.celnet.payment.form.IAppInterfaceForm;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.service.IAppInterfaceService;
import com.shimao.celnet.payment.service.IInterfaceLogService;
import com.shimao.celnet.payment.service.PInvoiceBlueService;
import com.shimao.celnet.payment.util.FinalUtil;
import com.shimao.celnet.payment.util.GetJFSystemToken;
import com.shimao.celnet.payment.util.HttpSendPostUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

/**
 * Created by galen on 2019/1/2.
 * 手动推送发票
 */

@RestController
@RequestMapping("/invoice")
public class ManualPushInvoice {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IAppInterfaceService iAppInterfaceService;

    @Autowired
    private PInvoiceBlueService pInvoiceBlueService;

    @Autowired
    private GetJFSystemToken getJFSystemToken;

    @Autowired
    private HttpSendPostUtil httpSendPostUtil;

    @Autowired
    private CallOut_invoiceQueryOutController callOut_invoiceQueryOutController;

    @Autowired
    private CallOut_invOpenRetInfoOutController callOut_invOpenRetInfoOutController;

    @Autowired
    private CallOut_invPdfOutController callOut_invPdfOutController;
    @Autowired
    private IInterfaceLogService iInterfaceLogService;

    @RequestMapping("/manualPash")
    public String manualPushInvice(String invoiceNo,String serial_no) {

        JSONObject RetJsonObject = new JSONObject();
        PInvoiceBlue pInvoiceBlue = pInvoiceBlueService.getInvoiceByinvoiceNo(invoiceNo);
        if(pInvoiceBlue == null){
            pInvoiceBlue = pInvoiceBlueService.getInvoiceBySerialNo(serial_no);
            PInvoiceBlue pInvoiceBlue1 = pInvoiceBlueService.getInvoiceByInvoiceYerminalCode(pInvoiceBlue.getInvoiceYerminalCode());
            pInvoiceBlue.setInvoiceCode(pInvoiceBlue1.getInvoiceCode());
            pInvoiceBlue.setInvoiceCheckCode(pInvoiceBlue1.getInvoiceCheckCode());
            pInvoiceBlue.setInvoiceQrcode(pInvoiceBlue1.getInvoiceQrcode());
            pInvoiceBlue.setInvoiceNo(invoiceNo);
        }
        String strUrl = null;
        InvoiceQueryResponse responseRet = callOut_invoiceQueryOutController.invoiceBlueQueryOut(pInvoiceBlue);

        //返回结果
        if (FinalUtil.INVOICE_TYPE_CODE_026.equals(pInvoiceBlue.getInvoiceTypeCode())) {
            strUrl = callOut_invPdfOutController.invPdfOut(pInvoiceBlue);
        }
        //如果查询结果查询失败，则直接查询本地的数据去回调。
        if (responseRet == null) {
            //返回结果
            RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(pInvoiceBlue, strUrl);
        } else {
            //返回结果
            RetJsonObject = callOut_invOpenRetInfoOutController.invOpenRetBlueInfoOut(responseRet, pInvoiceBlue, strUrl);
        }
        List<IAppInterfaceForm> iAppInterfaceFormsList = getRetUrl("20180117", "160");
        for (IAppInterfaceForm iAppInterfaceForm : iAppInterfaceFormsList) {
            //当要通知物业计费系统的时候，需要制定回调的接口应用
            if ("1005".equals(iAppInterfaceForm.getInterfaceId())) {
                /**
                 * 物业计费的接口验证规则
                 * 只接受来自物业的费用
                 */
                String token = getJFSystemToken.getJFSystemNewToken("20180117", iAppInterfaceForm.getInterfaceId());
                //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                String ret = httpSendPostUtil.sendPost(iAppInterfaceForm.getRequestUrl(), RetJsonObject, token, pInvoiceBlue.getAppKey(), iAppInterfaceForm.getInterfaceId());
                JSONObject retObj = JSONObject.fromObject(ret);
                //com.alibaba.fastjson.JSONObject retObj = JSON.parseObject(ret);
                String code = retObj.getString("errcode");
                if ("0".equals(code)) {
                    //保存数据库，更改状态
                    pInvoiceBlue.setIsPush("1");
                    pInvoiceBlueService.update(pInvoiceBlue);

                }
                RetJsonObject = retObj;

            }
        }
        return RetJsonObject.toString();
    }

    public List<IAppInterfaceForm> getRetUrl(String pAppKey, String pInterfaceId) {
        //============1,根据app_key和interface_id查询出i_app_interface表的guid====
        IAppInterfaceForm iAppInterfaceForm = iAppInterfaceService.getInfoByAppIfKey(pAppKey, pInterfaceId);
        //============3，根据guid，查询i_app_interface_asyn表所有需要回调的系统的returnUrl====
        logger.info("guid" + iAppInterfaceForm.getGuid());
        List<IAppInterfaceForm> iAppInterfaceFormList = iAppInterfaceService.getRetAppInterface(iAppInterfaceForm.getGuid());

        return iAppInterfaceFormList;
    }
}
